package view;

import java.util.ArrayList;
import mdes.slick.sui.Button;
import mdes.slick.sui.CheckBox;
import mdes.slick.sui.Container;
import mdes.slick.sui.Display;
import mdes.slick.sui.event.ActionEvent;
import mdes.slick.sui.event.ActionListener;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Music;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;
import org.newdawn.slick.state.transition.Transition;

public class MisterJumpMenu extends BasicGameState implements ActionListener {
	private int stateID = -1;
	private Image menuBackgroundImage = null;
	private Display display = null;
	    
	private Button[] buttons = null;	
    private int buttonYLocations[] = {77,260,341};
    private int buttonXLocations[] = {25,83,169};
    private CheckBox soundCheckBox  = null;
	private boolean playBtnPressed = false;
	private boolean optionBtnPressed = false;
	private boolean creditsBtnPressed = false;
    private boolean exitBtnPressed = false;
    
    private Music backgroundMusic ;
	private boolean isMusicOn = true;
	
    MisterJumpMenu( int stateID )  { this.stateID = stateID;}
	    
	     
    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
    	menuBackgroundImage = new Image("data/menu_background.png");
    	backgroundMusic = new Music("/data/bm.xm");
    	this.setMusicOn(true);
		display = new Display(gc);
	    buttons = new Button[3];
	    ArrayList buttonNames = new ArrayList(); buttonNames.add(0, "Play"); buttonNames.add(1,"Credits"); buttonNames.add(2,"Exit");
	        
	    Container container = new Container(); container.setSize(320, 512); container.setLocation(0, 0); container.setOpaque(false); 
	        
	    for (int i = 0; i < buttons.length; i++) {
	    	buttons[i] = new Button(buttonNames.get(i).toString());
			buttons[i].setLocation(buttonXLocations[i],buttonYLocations[i]);
			buttons[i].addActionListener(this);
			buttons[i].setActionCommand(buttonNames.get(i).toString());
			buttons[i].pack();
			container.add(buttons[i]);
		}
	    
	   	soundCheckBox = new CheckBox("Ton aus/an");
		soundCheckBox.setLocation(10,480);
		soundCheckBox.setName("soundCheckbox");
		soundCheckBox.addActionListener(this);
		soundCheckBox.setSelected(isMusicOn);
		soundCheckBox.pack();
	    container.add(soundCheckBox);    
	    display.add(container);
	   }
	    
    public void render(GameContainer gc, StateBasedGame sbg, Graphics gc1) throws SlickException {
    	menuBackgroundImage.draw(0, 0);
	    display.render(gc, gc1);
	    }
	     
	        public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
	        	if (soundCheckBox.isSelected() == true) {
	        		this.setMusicOn(true);
				}
	        	else {
					this.setMusicOn(false);
				}
	        	if (playBtnPressed) {
					sbg.enterState(MisterJumpMain.GAMEPLAYSTATE,new FadeOutTransition(),new FadeInTransition());
					playBtnPressed = false;
				}else if (creditsBtnPressed) {
					sbg.enterState(MisterJumpMain.CREDITSTATE, new FadeOutTransition(), new FadeInTransition());
					creditsBtnPressed = false;
				}else if (exitBtnPressed) {
					gc.exit();
					exitBtnPressed = false;
				}
	        	display.update(gc, 	delta);
	        }
     
	        public void setMusicOn(boolean value){
	        	if (value == true) {
					backgroundMusic.play();
				}
	        	else {
					backgroundMusic.pause();
				}
	        }
	        
	        public void actionPerformed(ActionEvent e) {
				if ("Play".equals(e.getActionCommand())) {
					playBtnPressed = true;
				}else if ("Options".equals(e.getActionCommand())) {
					optionBtnPressed = true;
				}else if ("Credits".equals(e.getActionCommand())) {
					creditsBtnPressed = true;
				}else if ("Exit".equals(e.getActionCommand())) {
					exitBtnPressed = true;
				}
			}
	        
	        @Override
	        public int getID() {
	            return stateID;
	        }
}